package com.jwaoo.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;

import com.dx.as.JWCache;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.string.MD5;
import com.dx.as.utils.DateUtil;

import java.io.File;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by jwaoo on 2017/7/3.
 */

public class AudioRecorder {
    private static AudioRecorder audioRecorder;
    private MediaRecorder myAudioRecorder;
    private boolean isRecording = false;
    private IAudioRecordCallback audioRecordCallback;
    private long starTime;
    private String filePath;
    private Handler uiHandler;
//    private Timer timer = new Timer();
    private int maxTime = 5 * 1000;

    public static synchronized AudioRecorder getAudioRecorder(Handler handler ,IAudioRecordCallback audioRecordCallback){
        if (audioRecorder == null){
            synchronized (AudioRecorder.class){
                if (audioRecorder == null){
                    audioRecorder = new AudioRecorder(handler,audioRecordCallback);
                }
            }
        }
        return audioRecorder;
    }

    public AudioRecorder(Handler handler,IAudioRecordCallback audioRecordCallback){
        this.audioRecordCallback = audioRecordCallback;
        this.uiHandler = handler;
    }

    public void initMedia(){
        if (myAudioRecorder == null){
            initAudioRecorder();
        }
    }

    private void initAudioRecorder(){
        myAudioRecorder=new MediaRecorder();
    }

    private void setConfig(){
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    }

    public void startRecord(){
        try {
            setConfig();
            starTime = System.currentTimeMillis();
            audioRecordCallback.onRecordStart();
            setSavePath();
            isRecording = true;
            myAudioRecorder.prepare();
            myAudioRecorder.start();
//            timer = new Timer();
//            timer.schedule(task,maxTime);
        }catch (Exception ex){
            ex.printStackTrace();
//            task.cancel();
//            timer.cancel();
        }
    }

    private void setSavePath(){
        String fileName = MD5.getStringMD5(DateUtil.getGMTUnixTimeByCalendar()+".");
        filePath = CommonUtils.getAudioDir(JWCache.getContext())+fileName;
        myAudioRecorder.setOutputFile(filePath);
    }

    public void completeRecord(boolean isCancel,long max){
        if (!isRecording) {
            return;
        }
        try{
            File file = new File(filePath);
            isRecording = false;
            myAudioRecorder.stop();
            if (isCancel) {
                if (file.exists()) file.delete();
            }else {
                long length = max != 0 ? max : (System.currentTimeMillis()-starTime);
                audioRecordCallback.onRecordSuccess(file,length);
            }
        }catch (Exception ex){
            uiHandler.post(new Runnable() {
                @Override
                public void run() {
                    audioRecordCallback.onRecordFail();
                }
            });
            ex.printStackTrace();
        }finally {
//            task.cancel();
//            timer.cancel();

            myAudioRecorder.reset();
        }
    }

    public void completeRecord(boolean isCancel){
        completeRecord(isCancel,0);
    }

    public void handleEndRecord(boolean isOk,long max){
        completeRecord(!isOk,max);
    }

    public void onDestory(){
        try{
            myAudioRecorder.stop();
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            myAudioRecorder.release();
            myAudioRecorder = null;
//            timer.cancel();
        }
    }

    public void doDelayedOption(){
        uiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

            }
        },120000);
    }

//    private TimerTask task = new TimerTask() {
//        @Override
//        public void run() {
//            uiHandler.post(new Runnable() {
//                @Override
//                public void run() {
//                    audioRecordCallback.onRecordReachedMaxTime(maxTime);
//                }
//            });
//        }
//    };
}
